/**
 * make a container ellapse / collapseable
 * 
 * @param string container id
 * @param float animation speed in seconds
 * @param bool store last state in session, default true = yes
 * 
 */
var phpmyContainerFoldable = function(containerid, animspeed, sessionstore){
    
    if (containerid == '' || !YAHOO.util.Dom.inDocument(containerid))
        return false;
    if (isNaN(animspeed))
        animspeed = 0.25;
    if (!sessionstore)
        sessionstore = true;
    this.status = 'open';
    this.containerid = containerid;
    this.duration = animspeed;
    this.sessionstore = sessionstore;
    
    // glue container
    var el = new YAHOO.util.Dom.get(containerid);
    var nel = document.createElement("div");
    nel.id = containerid + '_foldablecont';
    nel.style.position = 'relative';
    // insert in document.body as first element
    YAHOO.util.Dom.insertBefore(nel, el);

    // create ref
    var el = new YAHOO.util.Element(containerid);
    el.appendTo(containerid + '_foldablecont'); 
    
    // now insert indicator div
    var nel = document.createElement("div");
    //nel.style.position = 'relative';
    nel.id = containerid + '_foldableindicator';    
    // insert in document.body as first element
    YAHOO.util.Dom.insertBefore(nel, containerid);
    // insert clearfix after target element
    var nel = document.createElement("div");
    nel.id = containerid + '_foldablecontend';
    // insert in document.body as first element
    YAHOO.util.Dom.insertAfter(nel, containerid);
    
    // give class
    YAHOO.util.Dom.addClass(containerid + '_foldablecont', 'phpmyfoldablecontainer');
    YAHOO.util.Dom.addClass(containerid + '_foldableindicator', 'phpmyfoldableindicator');
    YAHOO.util.Dom.addClass(containerid + '_foldablecontend', 'phpmyfoldableend');
    // origi size
    this.oldheight = document.getElementById(containerid + '_foldablecont').offsetHeight;
    this.handlerClick = function (e, obj) {        
        if (obj.status == 'open') {
            // close
            obj.status = 'close';
            YAHOO.util.Dom.addClass(containerid + '_foldablecont', 'phpmyfoldablecontainerclosed');
            h = 0;
        } else {
            // open
            obj.status = 'open';
            h = parseFloat(obj.oldheight);
            YAHOO.util.Dom.removeClass(containerid + '_foldablecont', 'phpmyfoldablecontainerclosed');
        }
        var anim = new YAHOO.util.Anim(obj.containerid, {
            height: { to: h }
        });
        anim.duration = obj.duration; 
        anim.animate(); 
        if (obj.sessionstore)
            YAHOO.util.Cookie.set(obj.containerid, obj.status); 
    }
    // make clickable
    var el = new YAHOO.util.Element(containerid + '_foldableindicator');
    el.on('click', this.handlerClick, this);
    // check state
    if (this.sessionstore == true){        
        if (YAHOO.util.Cookie.get(this.containerid) == 'close')
            this.handlerClick(null, this);
    }    
}